package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.DateUtils;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.PageInfo;
import net.xju.common.utils.Result;
import net.xju.entity.Goods;
import net.xju.entity.Vo.GoodsVo;
import net.xju.service.GoodsService;
import net.xju.service.impl.GoodsServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

@RequestMapping("/api/goods")
public class GoodsController {

    GoodsService goodsService = new GoodsServiceImpl();
    // 组员写代码了
    // 组员又写代码
    // 组长写代码
    // 自己分支写代码
    // 组长在master写代码

    @RequestMapping("/pageList.do")
    public void pageList(HttpServletRequest req, HttpServletResponse resp) {
        // 分页参数
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr==null||pageNoStr.isEmpty()?1:Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr==null||pageSizeStr.isEmpty()?10:Integer.parseInt(pageSizeStr);
        // 条件
        Goods goods = new Goods();
        goods.setName(req.getParameter("name"));
        goods.setPubdate(DateUtils.parseStr2Date(req.getParameter("pubdate")));
        if (!req.getParameter("typeId").trim().isEmpty()) {
            goods.setTypeId(Integer.parseInt(req.getParameter("typeId")));
        }
       if (!req.getParameter("flag").trim().isEmpty()) {
           goods.setFlag(Integer.parseInt(req.getParameter("flag")));
       }
        PageInfo<GoodsVo> pageInfo = goodsService.pageList(pageNo,pageSize,goods);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }

    // 添加
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Goods goods = new Goods();
        goods.setName(req.getParameter("name"));
        goods.setPubdate(DateUtils.parseStr2Date(req.getParameter("pubdate")));
        // 添加参数校验
        String flagStr = req.getParameter("flag");
        if (flagStr == null || flagStr.trim().isEmpty()) {
            JSONUtil.printByJSON(resp, Result.fail("是否购买标志不能为空"));
        }
        goods.setFlag(Integer.parseInt(flagStr));

        String typeIdStr = req.getParameter("typeId");
        if (typeIdStr == null || typeIdStr.trim().isEmpty()) {
            JSONUtil.printByJSON(resp, Result.fail("类型ID不能为空"));
            return;
        }
        goods.setTypeId(Integer.parseInt(typeIdStr));


        goods.setIntro(req.getParameter("intro"));
        goods.setPicture(req.getParameter("picture"));


        String priceStr = req.getParameter("price");
        if (priceStr == null || priceStr.trim().isEmpty()) {
            JSONUtil.printByJSON(resp, Result.fail("价格不能为空"));
            return;
        }
        goods.setPrice(Integer.parseInt(priceStr));

        // 评分可以为空，默认为0
        String ratingStr = req.getParameter("rating");
        if (ratingStr != null && !ratingStr.trim().isEmpty()) {
            try {
                goods.setRating(Double.parseDouble(ratingStr));
            } catch (NumberFormatException e) {
                JSONUtil.printByJSON(resp, Result.fail("评分必须是有效数字"));
                return;
            }
        } else {
            goods.setRating(0.0); // 设置默认值
        }


        goods.setCreateTime(LocalDateTime.now());
        if (goodsService.insert(goods)) {
            JSONUtil.printByJSON(resp, Result.success("添加成功"));
        } else {
            JSONUtil.printByJSON(resp,Result.fail("添加失败"));
        }
    }
    // 删除
    @RequestMapping("/deleted.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        if (goodsService.delete(Integer.parseInt(id))) {
            JSONUtil.printByJSON(resp, Result.success("删除成功"));
        } else {
            JSONUtil.printByJSON(resp,Result.fail("删除失败"));
        }
    }

    // 更新
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Goods goods = new Goods();
        goods.setId(Integer.parseInt(req.getParameter("id")));
        goods.setName(req.getParameter("name"));
        goods.setPubdate(DateUtils.parseStr2Date(req.getParameter("pubdate")));
        if (!req.getParameter("flag").isEmpty()) {
            goods.setFlag(Integer.parseInt(req.getParameter("flag")));
        }
        if (!req.getParameter("typeId").trim().isEmpty()) {
            goods.setTypeId(Integer.parseInt(req.getParameter("typeId")));
        }
        goods.setIntro(req.getParameter("intro"));
        goods.setPicture(req.getParameter("picture"));
        if (!req.getParameter("price").trim().isEmpty()) {
            goods.setPrice(Integer.parseInt(req.getParameter("price")));
        }

        if (!req.getParameter("rating").trim().isEmpty()) {
            goods.setRating(Double.parseDouble(req.getParameter("rating")));
        }

        if (goodsService.update(goods)) {
            JSONUtil.printByJSON(resp, Result.success("更新成功"));
        } else {
            JSONUtil.printByJSON(resp,Result.fail("更新失败"));
        }

    }
}
